/*
 * Copyright (C) 冷锋
 */

#ifndef DEFINES_H
#define	DEFINES_H
#include <arpa/inet.h>
#include <list>
/************************************************************************/
/* socket ERROR                                                                     */
/************************************************************************/
#define  SOCKET_CLOSED	-100
#define	 SOCKET_EAGAIN	-101
#define  SOCKET_ERROR	-102
#define	 CONN_NOT_EXIST	-110

#define  ERROR_BUFFER_SIZE	-200
#define  SEND_DELAYED		-300 //延迟发送

#define  ERROR_PACKET_LEN	-400 //包体长度错误
#define  NOT_ENOUGH_CONNBUF		-500 //已经达到连接数上限

#define  READ_ONE_PACK_NOT_FINISHED	-501 //没有完全读取完一个协议包

#define  MAX_CONN_COUNT		1000 //最大连接数
#define  MAX_PACKET_LEN		1024 //协议包最大长度

#define LISTEN_BACKLOG	20		//连接等待队列长度
#define RATE_LIMIT		100*1024//每个连接最大速度限制为100K
#define IO_BUF_LEN		1024	//

#define READ_EVENT		0x01	//可读事件
#define WRITE_EVENT		0x02	//可写事件
#define CLEAR_EVENT		0x04	//边缘模式

#define MAX_EVENT	32
//数据包格式
#pragma pack(1)
typedef struct tagPacket
{
	int  len;//bodylen
	int  cmd;
	char body[0];
}Packet;
#pragma unpack()

struct tagHandleBuffer;
#pragma pack(1)
typedef struct tagConnection
{
	int			  fd;
	long long	  lSeq;			
	unsigned char bLongConn:1;	
	unsigned char bRead:1;		
	unsigned char bWrite:1;		
	unsigned char bAlive:1;	
	int			  iConnectTime;
	int			  iSendCount;	
	struct in_addr addr;	
	int			  iReadPos;
	char		  sReadBuf[MAX_PACKET_LEN];
	int			  iSendPos;
	std::list<tagHandleBuffer*>*pSendQueue;
	tagConnection* next;
}Connection;
#pragma unpack()

//待处理的数据包
#pragma pack(1)
typedef struct tagHandleBuffer
{
	Connection* pConn;
	long long	lSeq;//
	int			iPos;
	int			iSize;
	Packet		packet;
}HandleBuffer;
#pragma unpack()

extern bool ExistConn( Connection*pConn,long long seq );
extern int SendNow( HandleBuffer*buffer );
extern int SendQueue(std::list<HandleBuffer*>*pSendQueue);
extern int SetNonBlocking(int fd);

#endif
